package dao.impl;


import cqcet.aibd.soft.ObjectUtil;
import dao.UserDAO;
import model.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.List;

public class UserDAOImpl implements UserDAO {


    @Override
    public List<User> getUserList() throws Exception {
        //定义和方法相关的数据查询语句
        String sql = "select id, createTime, deleteTime, username, password, role, phone, isDel, balance, addressIds, email from user";
        //使用第三方库ObjectUtil,getList方法获取数据表中的所有记录
        return new ObjectUtil<User>().getList(sql, User.class);
    }

    @Override
    public int addUser(User user) {
        String sql = "insert into user (id, username, password, role, createTime, deleteTime, isDel) values(?,?,?,?,?,?,?)";
        return new ObjectUtil<User>().add(sql, user.getId(), user.getUsername(), user.getPassword(), user.getRole(), user.getCreateTime(), user.getDeleteTime(), user.getIsDel());
    }


    @Override
    public int updateUser(User user) {
        String sql = "update user set username=?,password=?,role=?,createTime=?,deleteTime=?,isDel=?,phone=?,balance=?,addressIds=?, email=? WHERE id = ?";
        return new ObjectUtil<User>().update(sql, user.getUsername(), user.getPassword(), user.getRole(), user.getCreateTime(), user.getDeleteTime(), user.getIsDel(), user.getPhone(), user.getBalance(), user.getAddressIds(), user.getEmail(), user.getId());
    }

    @Override
    public User getUserById(String id) {
        String sql = "select id, createTime, deleteTime, username, password, role, phone, isDel, balance, addressIds,email from user";
        return new ObjectUtil<User>().getOne(sql, User.class, id);
    }

    @Override
    public List<User> getUserByName(String username) {
        String sql = "select id, createTime, deleteTime, username, password, role, phone, isDel, balance, addressIds,email from user WHERE username like ?";
        return new ObjectUtil<User>().getList(sql, User.class, "%" + username + "%");
    }

    @Override
    public void deleteUser(String id) {
        String sql = "delete from user where id=?";
        new ObjectUtil<User>().delete(sql, id);
    }

    @Override
    public User login(String username, String password) {
        String sql = "select id, createTime, deleteTime, username, password, role, phone, isDel, balance, addressIds, email from user where username = ? and password = ? limit 1";

        return new ObjectUtil<User>().getOne(sql, User.class, username, password);
    }

    @Override
    public int updateUserBalance(Connection con, User user, Integer flag, Double num) throws Exception{

        String sql = null;
        switch (flag) {
            case 1:
                sql = "update user set balance = balance + ? where id = ? ";
                break;
            case 2:
                sql = "update user set balance = balance - ? where id = ? ";
                break;
        }

        PreparedStatement ps = null;

        ps = con.prepareStatement(sql);
        System.out.println(num);
        ps.setDouble(1, num);
        ps.setString(2, user.getId());
        return ps.executeUpdate();
    }


    @Override
    public int updateUserBalance(User user, Integer flag, Double num) {
        String str = "balance = balance ";
        switch (flag) {
            case 1:
                str += "+ ? ";
                break;
            case 2:
                str += "- ?";
                break;
            default:
                str += "+ ?";
        }
        String sql = "update user set " + str + " where id = ?";
        return new ObjectUtil<User>().update(sql, num, user.getId());
    }


    @Override
    public User getUserByEmail(String email) {
        String sql = "select * from user where email = ? limit 1";
        return new ObjectUtil<User>().getOne(sql, User.class, email);
    }
}
